package com.msu.nava.activity;

import android.app.ProgressDialog;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.msu.nava.R;
import com.msu.nava.adpter.MedicineAdapter;
import com.msu.nava.consts.URLs;
import com.msu.nava.modle.Medicine;
import com.msu.nava.modle.ResultSheet;
import com.msu.nava.util.GsonUtil;
import com.msu.nava.util.HttpUtil;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ResultActivity extends BaseActivity {
  private static final String MSURL = URLs.REMOTE_MS;
  private static String TAG = "ResultActivity";
  private ListView resListView;
  private String[] data = {
    "风寒感冒颗粒",
    "通宣理肺丸",
    "三九感冒灵",
    "银翘解毒片",
    "双黄消炎片",

  };
  private ProgressDialog progressDialog;
  private String userAnswerSheet;
  private TextView sickname;
  private TextView restv ;


  @Override
  public void onCreate(Bundle savedInstanceState) {
    Log.d("resstart", "resActivity");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    initView();
  }

  private void getData() {
    userAnswerSheet = getIntent().getStringExtra("data");
    Log.d("usa", "getData: " + userAnswerSheet);
  }

  public void initView() {
    resListView = (ListView) findViewById(R.id.reslistview);
    sickname = (TextView) findViewById(R.id.sickname);
    restv = (TextView) findViewById(R.id.restv);//

    //获得答题卡
    getData();
    //从服务器获取数据(远程诊断)
    //queryFromServer(REMOTEURL);
    queryFromServer(MSURL);
  }

  private void queryFromServer(String address) {
    showProgressDialog();
    HttpUtil.sendRecipeRequest(address, userAnswerSheet, new Callback() {
      @Override
      public void onFailure(Call call, IOException e) {
        Log.d("recipeerr", "获取诊断失败:" + e.getMessage());
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            closeProgressDialog();
            Toast.makeText(ResultActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
          }
        });
      }

      @Override
      public void onResponse(Call call, Response response) throws IOException {
        final String text = response.body().string();
        Log.d("answer", "rs:" + text);
        //获取到结果
        final ResultSheet rs = GsonUtil.getResultSheet(text);
        if (rs == null) {
          Log.d("rs", "result null");
          sickname.setText("数据获取失败");
          return;
        }
        //获取到推荐药物
        final List<Medicine> medicines = rs.getMedicines();
        if (medicines != null && medicines.size() != 0) {
          Log.d("hasm", "onResponse: "+medicines.get(0).getComposition());
          runOnUiThread(new Runnable() {
            @Override
            public void run() {
              closeProgressDialog();
              //必须在主线程上更新view
              //显示病的名字
              sickname.setText(rs.getSickname());
              //初始化medicine适配器
              MedicineAdapter medicineAdapter = new MedicineAdapter(ResultActivity.this, R.layout.drugitem, medicines);
              resListView.setAdapter(medicineAdapter);
              //Toast.makeText(ResultActivity.this, "请求成功！", Toast.LENGTH_SHORT).show();
            }
          });
        }else{
          Log.d("0mds", "onResponse: can't get medicines");
        }

      }
    });

  }

  /**
   * 显示进度对话框
   */
  private void showProgressDialog() {
    if (progressDialog == null) {
      progressDialog = new ProgressDialog(ResultActivity.this);
      progressDialog.setMessage("正在加载...");
      progressDialog.setCanceledOnTouchOutside(false);
    }
    progressDialog.show();
  }

  /**
   * 关闭进度对话框
   */
  private void closeProgressDialog() {
    if (progressDialog != null) {
      progressDialog.dismiss();
    }
  }
}
